package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.statement.entity.StatementStock;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseBussinessArea;
import com.maoye.ossp.sys.base.entity.SysBaseCity;
import com.maoye.ossp.sys.base.entity.SysBaseCountry;
import com.maoye.ossp.sys.base.entity.SysBaseShoppingMall;
import com.maoye.ossp.sys.base.service.SysBaseBussinessService;
import com.maoye.ossp.sys.base.service.SysBaseCountryService;
import com.maoye.ossp.sys.base.service.SysBaseShoppingMallService;
import com.maoye.ossp.sys.site.utils.BeanToMap;

/**
 * 商场商圈controller类
 * @author liangtao
 * @date   2014-5-4
 */
@Controller
@RequestMapping(value = "sys/base/bussinessShopping/")
public class SysBaseBussinessShoppingController extends BaseController {
	@Resource
	private SysBaseBussinessService sysBaseBussinessService; 
	
	@Resource
	private SysBaseShoppingMallService sysBaseShoppingMallService; 
	/**
	 * 获得商圈列表
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "getBussinessAreaList")
	public String getBussinessArealist(SysBaseBussinessArea sysBaseBussinessArea,
			HttpServletRequest request, Model model) {
		QueryFilter queryFilter = getQuerFilter(request,sysBaseBussinessArea);
		Page<SysBaseBussinessArea> page = (Page<SysBaseBussinessArea>) sysBaseBussinessService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("sysBaseBussinessArea",sysBaseBussinessArea);
		return resultPage("/sys/base/bussinessArea/getBussinessAreaList", model, request);
	}
	
	@RequestMapping(value = "addBussinessArea")
	public String addBussinessArea(HttpServletRequest request, Model model)
			throws IOException {
		Map map = new HashMap();
		SysBaseBussinessArea sysBaseBussinessArea = new SysBaseBussinessArea();
		model.addAttribute("sysBaseBussinessArea",sysBaseBussinessArea);
		return "/sys/base/bussinessArea/add";
	}
	/**
	 * 验证商圈编码
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="checkBussinessAreaCode")
	public void checkBussinessAreaCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		map.put("code",code);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseBussinessService.checkBussinessAreaCode(map)){
			json.put("resCode",0); //商圈编号可用
		}else{
			json.put("resCode",1);//商圈编号不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
   
	/**
	 * 保存商圈信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "saveBussinessArea")
	public String saveBussinessArea(SysBaseBussinessArea sysBaseBussinessArea,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		sysBaseBussinessArea.setId(idGenerator.getUId());
		sysBaseBussinessArea.setCode(sysBaseBussinessArea.getCode().replaceAll(",","").toString());
		//设置创建人
		sysBaseBussinessArea.setCreateBy("");
		//设置修改人
		sysBaseBussinessArea.setUpdateBy("");
		sysBaseBussinessService.saveBussinessArea(sysBaseBussinessArea);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getBussinessAreaList";
	}
	
	/*
	*//**
	 * 查看商圈信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="viewBussinessArea")
	public String viewBussinessArea(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String cityName=request.getParameter("cityName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseBussinessArea bussinessArea=null;
		bussinessArea=sysBaseBussinessService.getBussinessAreaByCode(map);
		bussinessArea.setCityName(cityName);
		model.addAttribute("bussinessArea",bussinessArea);
		return "/sys/base/bussinessArea/view";
	}
	
	@RequestMapping(value ="updateBussinessArea")
	public String updateBussinessArea(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String cityName=request.getParameter("cityName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseBussinessArea bussinessArea=null;
		bussinessArea=sysBaseBussinessService.getBussinessAreaByCode(map);
		model.addAttribute("sysBaseBussinessArea",bussinessArea);
		model.addAttribute("cityName",cityName);
		return "/sys/base/bussinessArea/update";
	}
	
	/*
	*//**
	 * 保存修改商圈信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="saveUpdateBussinessArea")
	public String saveUpdateBussinessArea(HttpServletRequest request,HttpServletResponse response,SysBaseBussinessArea bussinessArea,RedirectAttributes redirectAttributes){
		//设置创建人
		bussinessArea.setCreateBy("");
		//设置修改
		bussinessArea.setUpdateBy("");
		sysBaseBussinessService.saveUpdateBussinessArea(bussinessArea);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getBussinessAreaList";
	}
	
	/*
	*//**
	 * 删除商圈信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */ 
	@RequestMapping(value ="removeBussinessArea")
	public String removeBussinessArea(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		Map map = new HashMap();
		String code=request.getParameter("code");
		map.put("code",code);
		sysBaseBussinessService.removeBussinessArea(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getBussinessAreaList";
	}
	
	
	/**
	 * 获得商场列表
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "getShoppingMallList")
	public String getShoppingMallList(SysBaseShoppingMall sysBaseShoppingMall,
			HttpServletRequest request, Model model) {
		QueryFilter queryFilter = getQuerFilter(request,sysBaseShoppingMall);
		Page<SysBaseShoppingMall> page = (Page<SysBaseShoppingMall>) sysBaseShoppingMallService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("sysBaseShoppingMall",sysBaseShoppingMall);
		return resultPage("/sys/base/shoppingMall/getShoppingMallList", model, request);
	}
	
	
	
	@RequestMapping(value = "addShoppingMall")
	public String addShoppingMall(HttpServletRequest request, Model model)
			throws IOException {
		Map map = new HashMap();
		SysBaseShoppingMall sysBaseShoppingMall = new SysBaseShoppingMall();
		model.addAttribute("sysBaseShoppingMall",sysBaseShoppingMall);
		return "/sys/base/shoppingMall/add";
	}
	/**
	 * 验证商圈编码
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="checkShoppingMallCode")
	public void checkShoppingMallCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		map.put("code",code);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseShoppingMallService.checkShoppingMallCode(map)){
			json.put("resCode",0); //商圈编号可用
		}else{
			json.put("resCode",1);//商圈编号不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
   
	/**
	 * 保存商圈信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "saveShoppingMall")
	public String saveShoppingMall(SysBaseShoppingMall sysBaseShoppingMall,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		sysBaseShoppingMall.setId(idGenerator.getUId());
		sysBaseShoppingMall.setCode(sysBaseShoppingMall.getCode().replaceAll(",","").toString());
		//设置创建人
		sysBaseShoppingMall.setCreateBy("");
		//设置修改人
		sysBaseShoppingMall.setUpdateBy("");
		sysBaseShoppingMallService.saveShoppingMall(sysBaseShoppingMall);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getShoppingMallList";
	}
	
	/*
	*//**
	 * 查看商圈信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="viewShoppingMall")
	public String viewShoppingMall(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String bussinessAreaName=request.getParameter("bussinessAreaName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseShoppingMall shoppingMall=null;
		shoppingMall=sysBaseShoppingMallService.getShoppingMallByCode(map);
		shoppingMall.setBussinessAreaName(bussinessAreaName);
		model.addAttribute("shoppingMall",shoppingMall);
		return "/sys/base/shoppingMall/view";
	}
	
	@RequestMapping(value ="updateShoppingMall")
	public String updateShoppingMall(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String bussinessAreaName=request.getParameter("bussinessAreaName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseShoppingMall shoppingMall=null;
		shoppingMall=sysBaseShoppingMallService.getShoppingMallByCode(map);
		model.addAttribute("sysBaseShoppingMall",shoppingMall);
		model.addAttribute("bussinessAreaName",bussinessAreaName);
		return "/sys/base/shoppingMall/update";
	}
	
	/*
	*//**
	 * 保存修改商圈信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="saveUpdateShoppingMall")
	public String saveUpdateShoppingMall(HttpServletRequest request,HttpServletResponse response,SysBaseShoppingMall shoppingMall,RedirectAttributes redirectAttributes){
		//设置创建人
		shoppingMall.setCreateBy("");
		//设置修改
		shoppingMall.setUpdateBy("");
		sysBaseShoppingMallService.saveUpdateShoppingMall(shoppingMall);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getShoppingMallList";
	}
	
	/*
	*//**
	 * 删除商圈信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */ 
	@RequestMapping(value ="removeShoppingMall")
	public String removeShoppingMall(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		Map map = new HashMap();
		String code=request.getParameter("code");
		map.put("code",code);
		sysBaseShoppingMallService.removeShoppingMall(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getShoppingMallList";
	}
	
}
